import 'package:hktk_app/ienum/login/suc_return_enum.dart';
import 'package:hktk_app/ienum/practice/one_do_source_enum.dart';
import 'package:hktk_app/ienum/subject/collection_type_enum.dart';
import 'package:hktk_app/model/course/video_info_model.dart';
import 'package:hktk_app/model/home/column_model.dart';
import 'package:hktk_app/model/industry/category_model.dart';
import 'package:hktk_app/model/login/config_model.dart';
import 'package:hktk_app/model/package/order_model.dart';
import 'package:hktk_app/model/package/package_model.dart';
import 'package:hktk_app/model/subject/question_collection_model.dart';
import 'package:hktk_app/model/subject/submit_paper_model.dart';
import 'package:hktk_app/pages/action/list.dart';
import 'package:hktk_app/pages/action/toggle.dart';
import 'package:hktk_app/pages/action/video.dart';
import 'package:hktk_app/pages/course/course_detail_page.dart';
import 'package:hktk_app/pages/course/course_package_detail.dart';
import 'package:hktk_app/pages/course/course_video_page1.dart';
import 'package:hktk_app/pages/free/free_source.dart';
import 'package:hktk_app/pages/login/automatic2_page.dart';
import 'package:hktk_app/pages/login/hi_webview_widget.dart';
import 'package:hktk_app/pages/login/phone_page.dart';
import 'package:hktk_app/pages/login/reset_password_page.dart';
import 'package:hktk_app/pages/mine/activation_page.dart';
import 'package:hktk_app/pages/mine/calendar.dart';
import 'package:hktk_app/pages/mine/collection_page.dart';
import 'package:hktk_app/pages/mine/customer_service_page.dart';
import 'package:hktk_app/pages/mine/download_page.dart';
import 'package:hktk_app/pages/mine/more_page.dart';
import 'package:hktk_app/pages/mine/my_course_page.dart';
import 'package:hktk_app/pages/mine/my_note_page.dart';
import 'package:hktk_app/pages/mine/opinion_page.dart';
import 'package:hktk_app/pages/mine/question_bank_page.dart'
    show QuestionBankPage;
import 'package:hktk_app/pages/mine/resource_page.dart';
import 'package:hktk_app/pages/mine/set_up_page.dart';
import 'package:hktk_app/pages/mine/update_password_page.dart';
import 'package:hktk_app/pages/mine/wrong_question_page.dart';
import 'package:hktk_app/pages/order/order_detail.dart' show OrderDetailPage;
import 'package:hktk_app/pages/order/order_list.dart';
import 'package:hktk_app/pages/package/order_payment_page.dart'
    show OrderPaymentPage;
import 'package:hktk_app/pages/package/package_detail.dart';
import 'package:hktk_app/pages/package/package_list.dart';
import 'package:hktk_app/pages/package/pay_success_page.dart';
import 'package:hktk_app/pages/preview/image_preview_page.dart';
import 'package:hktk_app/pages/preview/pdf_preview_page.dart';
import 'package:hktk_app/pages/question/analysis_question_page.dart';
import 'package:hktk_app/pages/question/chapter_question_page.dart';
import 'package:hktk_app/pages/question/collection_question_page.dart';
import 'package:hktk_app/pages/question/diary_page.dart';
import 'package:hktk_app/pages/question/random_question_page.dart';
import 'package:hktk_app/pages/question/record_question_page.dart';
import 'package:hktk_app/pages/question/result_page.dart';
import 'package:hktk_app/pages/question/single_question_page.dart';
import 'package:hktk_app/pages/select/select_exam_page.dart';
import 'package:hktk_app/pages/select/select_subject_page.dart';
import 'package:hktk_app/pages/select/select_type_page.dart';
import 'package:hktk_app/pages/subject/doing_sift_page.dart';
import 'package:hktk_app/pages/subject/error_sift_page.dart';
import 'package:hktk_app/pages/subject/paper_detail_page.dart';
import 'package:hktk_app/pages/subject/paper_library.dart';
import 'package:hktk_app/pages/subject/test_paper_page.dart';
import 'package:hktk_app/pages/subject/true_question_page.dart';
import 'package:hktk_app/pages/video/save_video_list.dart';
import 'package:hktk_app/pages/video/video_list.dart';
import 'package:hktk_app/pages/video/video_records.dart';

// import 'package:hktk_app/test_page/my_video.dart';
import 'package:hktk_app/util/data_util.dart';
import 'package:hktk_app/widget/subject/chapter_practice_list.dart';
import 'model/practice/practice_model.dart';
import 'navigotr/tab_navigator.dart';
import 'package:flutter/material.dart';

final routes = {
  // "/test":(context)=>MyVideo(),
  "/free": (context) => FreeSource(),
  // tabs对应的页面
  "/tab": (context) {
    if (ModalRoute.of(context)!.settings.arguments != null &&
        (ModalRoute.of(context)!.settings.arguments as Map)
            .containsKey("pageIndex")) {
      int subIndex = 0;
      if ((ModalRoute.of(context)!.settings.arguments as Map)
          .containsKey("subIndex")) {
        subIndex = (ModalRoute.of(context)!.settings.arguments
            as Map)["subIndex"] as int;
      }
      return TabNavigator(
        pageIndex:
            (ModalRoute.of(context)!.settings.arguments as Map)["pageIndex"],
        subIndex: subIndex,
      );
    }
    return TabNavigator();
  },
  // 协议隐式预览
  "/h5": (context) => HiWebViewWidget(
        url: (ModalRoute.of(context)!.settings.arguments as Map)["url"]
            as String,
        title: (ModalRoute.of(context)!.settings.arguments as Map)["title"]
            as String,
        showClose: (ModalRoute.of(context)!.settings.arguments
            as Map)["showClose"] as bool,
        btnPadding: (ModalRoute.of(context)!.settings.arguments
            as Map)["btnPadding"] as double?,
      ),
  // 验证码登录
  "/phone": (context) => PhonePage(
      appConfig: (ModalRoute.of(context)!.settings.arguments
          as Map)["appConfig"] as AppConfig,
      closeVisitors: (ModalRoute.of(context)!.settings.arguments
          as Map)["closeVisitors"] as bool,
      sucReturn: (ModalRoute.of(context)!.settings.arguments
          as Map)["sucReturn"] as SucReturnEnum,
      onPage: (ModalRoute.of(context)!.settings.arguments as Map)["onPage"]
          as Widget?),
  // 一键登录
  "/login": (context) {
    Map? map = ModalRoute.of(context)!.settings.arguments as Map?;
    if (map != null) {
      return Automatic2Page(
          closeVisitors: map["closeVisitors"] ?? true,
          sucReturn: map["sucReturn"] ?? SucReturnEnum.toHome,
          onPage: map["onPage"]);
    }

    return Automatic2Page();
  },
  // 重置密码
  "/resetPassword": (context) => ResetPasswordPage(
      phoneStr: (ModalRoute.of(context)!.settings.arguments as Map)["phoneStr"]
          as String),
  "/updatePassword": (context) => UpdatePasswordPage(),
  // 设置页
  "/setUp": (context) => SetUpPage(),
  // 显示栏目标签，点击栏目标签直接进入
  "/mine/questionBank": (context) => QuestionBankPage(),
  "/mine/orderList": (context) => OrderListPage(),
  "/mine/orderDetail": (context) => OrderDetailPage(
        orderId: (ModalRoute.of(context)!.settings.arguments as Map)["orderId"]
            as String,
        packageName: (ModalRoute.of(context)!.settings.arguments
            as Map)["packageName"] as String,
      ),
  "/select-exam": (context) => SelectExamPage(),
  "/select-category": (context) => SelectSubjectPage(
        subjectList: (ModalRoute.of(context)!.settings.arguments
            as Map)["subjectList"] as List<CategoryModel>,
        buyCategorys: (ModalRoute.of(context)!.settings.arguments
            as Map)["buyCategorys"] as List<CategoryModel>?,
        pageIndex: (ModalRoute.of(context)!.settings.arguments
            as Map)["pageIndex"] as int,
        pageName: (ModalRoute.of(context)!.settings.arguments
            as Map)["pageName"] as String,
        arguments: (ModalRoute.of(context)!.settings.arguments as Map)
                .containsKey("arguments")
            ? (ModalRoute.of(context)!.settings.arguments as Map)["arguments"]
            : null,
      ),
  "/select-type": (context) => SelectTypePage(
        examId: (ModalRoute.of(context)!.settings.arguments as Map)["examId"]
            as String,
        subjectList: (ModalRoute.of(context)!.settings.arguments
            as Map)["subjectList"] as List<CategoryModel>,
        buyCategorys: (ModalRoute.of(context)!.settings.arguments
            as Map)["buyCategorys"] as List<CategoryModel>?,
        pageIndex: (ModalRoute.of(context)!.settings.arguments
            as Map)["pageIndex"] as int,
      ),
  "/package-list": (context) => PackageList(),
  "/preview-pdf": (context) => PdfPreviewPage(
      url: (ModalRoute.of(context)!.settings.arguments as Map)["url"] as String,
      name:
          (ModalRoute.of(context)!.settings.arguments as Map)["name"] as String,
      type:
          (ModalRoute.of(context)!.settings.arguments as Map)["type"] as String,
      netWork: (ModalRoute.of(context)!.settings.arguments as Map)["netWork"]
          as bool),
  "/preview-img": (context) {
    return ImagePreviewPage(
      url: (ModalRoute.of(context)!.settings.arguments as Map)["url"] as String,
      name:
          (ModalRoute.of(context)!.settings.arguments as Map)["name"] as String,
      type:
          (ModalRoute.of(context)!.settings.arguments as Map)["type"] as String,
      netWork: (ModalRoute.of(context)!.settings.arguments as Map)["netWork"] ??
          true,
      showTips:
          (ModalRoute.of(context)!.settings.arguments as Map)["showTips"] ??
              false,
      columnId:
          (ModalRoute.of(context)!.settings.arguments as Map)["columnId"] ?? "",
      resId: (ModalRoute.of(context)!.settings.arguments as Map)["resId"] ?? "",
      videoRes:
          (ModalRoute.of(context)!.settings.arguments as Map)["videoRes"] ??
              false,
    );
  },
  "/preview-txt": (context) {
    return ImagePreviewPage(
      url: (ModalRoute.of(context)!.settings.arguments as Map)["url"] as String,
      name:
          (ModalRoute.of(context)!.settings.arguments as Map)["name"] as String,
      type:
          (ModalRoute.of(context)!.settings.arguments as Map)["type"] as String,
      netWork: (ModalRoute.of(context)!.settings.arguments as Map)["netWork"] ??
          true,
      showTips:
          (ModalRoute.of(context)!.settings.arguments as Map)["showTips"] ??
              false,
      columnId:
          (ModalRoute.of(context)!.settings.arguments as Map)["columnId"] ?? "",
      resId: (ModalRoute.of(context)!.settings.arguments as Map)["resId"] ?? "",
      videoRes:
          (ModalRoute.of(context)!.settings.arguments as Map)["videoRes"] ??
              false,
    );
  },
  // 考试日历
  "/mine/calendar": (context) => CalendarPage(),
  "/package-detail": (context) => PackageDetail(
      agentPrice: (ModalRoute.of(context)!.settings.arguments as Map)['agentPrice']
          as int,
      cover: (ModalRoute.of(context)!.settings.arguments as Map)['cover']
          as String?,
      days: (ModalRoute.of(context)!.settings.arguments as Map)['days'] as int,
      discountPrice: (ModalRoute.of(context)!.settings.arguments
          as Map)['discountPrice'] as int,
      enable:
          (ModalRoute.of(context)!.settings.arguments as Map)['enable'] as bool,
      examId: (ModalRoute.of(context)!.settings.arguments as Map)['examId']
          as String,
      examName: (ModalRoute.of(context)!.settings.arguments as Map)['examName']
          as String,
      examType: (ModalRoute.of(context)!.settings.arguments as Map)['examType']
          as String?,
      id: (ModalRoute.of(context)!.settings.arguments as Map)['id'] as String,
      groupIds: (ModalRoute.of(context)!.settings.arguments as Map)['groupIds']
          as String?,
      industryId: (ModalRoute.of(context)!.settings.arguments
          as Map)['industryId'] as String,
      industryName: (ModalRoute.of(context)!.settings.arguments
          as Map)['industryName'] as String?,
      name:
          (ModalRoute.of(context)!.settings.arguments as Map)['name'] as String,
      price:
          (ModalRoute.of(context)!.settings.arguments as Map)['price'] as int,
      // recommend:String,
      remark: (ModalRoute.of(context)!.settings.arguments as Map)['remark']
          as String,
      tmPrice:
          (ModalRoute.of(context)!.settings.arguments as Map)['tmPrice'] as int,
      tagIds: (ModalRoute.of(context)!.settings.arguments as Map)['tagIds']
          as List<String?>,
      tagNameList: (ModalRoute.of(context)!.settings.arguments as Map)['tagNameList'] as List<String?>,
      tagTypeList: (ModalRoute.of(context)!.settings.arguments as Map)['tagTypeList'] as List<String?>),
/*  price: _totalMoney,
  skuList: list,
  package: widget.package,
  index: widget.index,
  orderModel: orderModel,
  iosPrice: _showMoney,
  payHide: widget.payHide,*/
  "/order/pay": (context) => OrderPaymentPage(
        price: (ModalRoute.of(context)!.settings.arguments as Map)['price']!
            as double,
        skuList: (ModalRoute.of(context)!.settings.arguments as Map)['skuList']
            as List<SkuModel>,
        package: (ModalRoute.of(context)!.settings.arguments as Map)['package']
            as PackageModel,
        index:
            (ModalRoute.of(context)!.settings.arguments as Map)['index'] as int,
        iosPrice: (ModalRoute.of(context)!.settings.arguments
            as Map)['iosPrice'] as double,
        payHide: (ModalRoute.of(context)!.settings.arguments as Map)['payHide']
            as bool,
        orderModel: (ModalRoute.of(context)!.settings.arguments
            as Map)['orderModel'] as OrderModel,
      ),
  "/order/pay/success": (context) => PaySuccessPage(),
  "/morePage": (context) => MorePage(
      titleName: (ModalRoute.of(context)!.settings.arguments
          as Map)['titleName'] as String,
      list: (ModalRoute.of(context)!.settings.arguments as Map)['list']
          as List<ColumnModel>),
  "/mine/customer_service": (context) => CustomerServicePage(),
  "/mine/active": (context) => ActivationPage(),
  "/mine/option": (context) => OpinionPage(),
  "/mine/download": (context) {
    return DownloadPage();
  },
  "/mine/course": (context) {
    return MyCoursePage();
  },
  "/mine/resource": (context) {
    return ResourcePage();
  },
  "/video-list": (context) {
    return VideoListPage();
  },
  "/chapter-question": (context) {
    return ChapterQuestionPage(
      id: (ModalRoute.of(context)!.settings.arguments as Map)["id"] as String,
      collType: (ModalRoute.of(context)!.settings.arguments as Map)["collType"]
          as CollectionTypeEnum,
      columnId: (ModalRoute.of(context)!.settings.arguments as Map)["columnId"]
          as String,
      cId: (ModalRoute.of(context)!.settings.arguments as Map)["cId"] as String,
      rType: (ModalRoute.of(context)!.settings.arguments as Map)["rType"],
      practice:
          (ModalRoute.of(context)!.settings.arguments as Map)["practice"] ??
              true,
      examTime:
          (ModalRoute.of(context)!.settings.arguments as Map)["examTime"] ?? 0,
    );
  },

  "/records-question": (context) {
    return RecordQuestionPage(
      practiceModel: (ModalRoute.of(context)!.settings.arguments
          as Map)["practiceModel"] as PracticeModel,
    );
  },
  "/diary": (context) {
    return DiaryPage();
  },
  "/result": (context) {
    var submitModel = (ModalRoute.of(context)!.settings.arguments
        as Map)["submitModel"] as SubmitPaperModel;
    var paperRecords = (ModalRoute.of(context)!.settings.arguments
        as Map)["paperRecords"] as SubmitPaperModel;
    return ResultPage(submitModel: submitModel, paperRecords: paperRecords);
  },
  "/analysis-question": (context) {
    var submitPaper = (ModalRoute.of(context)!.settings.arguments
        as Map)["submitPaper"] as SubmitPaperModel;
    var collectionModel = (ModalRoute.of(context)!.settings.arguments
        as Map)["collectionModel"] as QuestionCollectionModel;
    var type =
        (ModalRoute.of(context)!.settings.arguments as Map)["type"] as bool;
    return AnalysisQuestionPage(
      submitPaper: submitPaper,
      type: type,
      collectionModel: collectionModel,
    );
  },
  "/course_video": (context) {
    return CourseVideoPage1(
      columnId:
          (ModalRoute.of(context)!.settings.arguments as Map)["columnId"] ?? "",
      topChapterId:
          (ModalRoute.of(context)!.settings.arguments as Map)["topChapterId"] ??
              "",
      videoId:
          (ModalRoute.of(context)!.settings.arguments as Map)["videoId"] ?? "",
      aliyunVideoId: (ModalRoute.of(context)!.settings.arguments
              as Map)["aliyunVideoId"] ??
          "",
      isHorizontal:
          (ModalRoute.of(context)!.settings.arguments as Map)["isHorizontal"] ??
              false,
      videoTitle:
          (ModalRoute.of(context)!.settings.arguments as Map)["videoTitle"] ??
              "",
      watchDuration: (ModalRoute.of(context)!.settings.arguments
              as Map)["watchDuration"] ??
          0,
    );
  },
  "/course-detail": (context) {
    return CourseDetailPage(
      infoModel: (ModalRoute.of(context)!.settings.arguments
          as Map)["infoModel"] as VideoInfoModel,
      chapterId: (ModalRoute.of(context)!.settings.arguments
          as Map)["chapterId"] as String,
      chapterName: (ModalRoute.of(context)!.settings.arguments
          as Map)["chapterName"] as String,
      columnId: (ModalRoute.of(context)!.settings.arguments as Map)["columnId"]
          as String,
      free: (ModalRoute.of(context)!.settings.arguments as Map)["free"] as bool,
    );
  },
  "/course-package-detail": (context) {
    return CoursePackageDetail();
  },
  "/paper-detail": (context) {
    return PaperDetailPage(
        id: (ModalRoute.of(context)!.settings.arguments as Map)["id"] as String,
        columnId: (ModalRoute.of(context)!.settings.arguments
            as Map)["columnId"] as String,
        recordId:
            (ModalRoute.of(context)!.settings.arguments as Map)["recordId"],
        index:
            (ModalRoute.of(context)!.settings.arguments as Map)["index"] as int,
        categoryId:
            (ModalRoute.of(context)!.settings.arguments as Map)["categoryId"]);
  },
  "/paper-library": (context) {
    return TrueQuestionPage(
        title: (ModalRoute.of(context)!.settings.arguments as Map)["title"]);
  },
  "/paper-library-list": (context) {
    return PaperLibrary(
      categoryId:
          (ModalRoute.of(context)!.settings.arguments as Map)["categoryId"],
      industryId:
          (ModalRoute.of(context)!.settings.arguments as Map)["industryId"],
      examId: (ModalRoute.of(context)!.settings.arguments as Map)["examId"],
      columnModel:
          (ModalRoute.of(context)!.settings.arguments as Map)["columnModel"],
    );
  },
  "/random-mode": (context) {
    return TestPaperPage(
      title: (ModalRoute.of(context)!.settings.arguments as Map)["title"],
      columnId: (ModalRoute.of(context)!.settings.arguments as Map)["columnId"],
      categoryId:
          (ModalRoute.of(context)!.settings.arguments as Map)["categoryId"],
    );
  },
  "/random-question": (context) {
    return RandomQuestionPage(
      recordsModel:
          (ModalRoute.of(context)!.settings.arguments as Map)["recordsModel"],
    );
  },
  "/video-records": (context) {
    return VideoRecordList();
  },
  "/save-video-list": (context) {
    return SaveVideoList();
  },
  "/single-question": (context) {
    return SingleQuestionPage(
      collectId:
          (ModalRoute.of(context)!.settings.arguments as Map)["collectId"],
      paperName:
          (ModalRoute.of(context)!.settings.arguments as Map)["paperName"],
      qId: (ModalRoute.of(context)!.settings.arguments as Map)["qId"],
      qType: (ModalRoute.of(context)!.settings.arguments as Map)["qType"],
      rType: (ModalRoute.of(context)!.settings.arguments as Map)["rType"],
      view: (ModalRoute.of(context)!.settings.arguments as Map)["view"] ?? true,
      oneDoSourceEnum: (ModalRoute.of(context)!.settings.arguments
              as Map)["oneDoSourceEnum"] ??
          OneDoSourceEnum.erveryDay,
    );
  },
  "/collection": (context) {
    return CollectionQuestionPage(
      collectId:
          (ModalRoute.of(context)!.settings.arguments as Map)["collectId"],
      collectionType:
          (ModalRoute.of(context)!.settings.arguments as Map)["collectionType"],
      type: (ModalRoute.of(context)!.settings.arguments as Map)["type"],
    );
  },
  "/wrong-question": (context) {
    return WrongQuestionPage();
  },
//   错题精选页面
  "/error-primary": (context) {
    return ErrorSiftPage();
  },

  /// 热点试题
  "/hot-question": (context) {
    return DoingSiftPage();
  },
  "/my-collection": (context) {
    return CollectionPage();
  },
  "/my-note": (context) {
    return MyNodePage();
  },
  "/action-guide": (context) =>ActionGuide(),
  "/toggle-guide": (context) =>ToggleGuide(),
  "/video-guide": (context) =>VideoGuide(),
};

//动态路由
// onGenerateRoute: (settings) {
// if (settings.name == '/') {
// return MaterialPageRoute(builder: (context) => HomePage());
// } else if (settings.name == '/detail') {
// return MaterialPageRoute(builder: (context) => DetailPage());
// }
// return null; // 未匹配的路由
// },

final List<String> _needLoginPages = [
  "WrongQuestionPage",
  "MyNodePage",
  "CollectionPage",
  "/mine/questionBank",
  "MyCoursePage",
  "ResourcePage",
  "ActivationPage",
  "DownloadPage",
  "ChapterQuestionPage",
  "WrongQuestionPage",
  "PaperDetailPage",
  "TestPaperPage",
];

class MyRouteObserver<R extends Route<dynamic>> extends RouteObserver<R> {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);

    // // 在这里可以添加拦截逻辑
    // if (shouldAllowNavigation(route)) {
    //   // 允许导航
    // } else {
    //   // 不允许导航，可以返回到上一个页面
    //   // Navigator.pop(route.navigator!.context);
    //
    // //   这是因为didPush方法是在导航器锁定的状态下调用的，此时不允许进行进一步的导航操作
    //   WidgetsBinding.instance.addPostFrameCallback((timeStamp){
    //   Navigator.pushNamed(route.navigator!.context,"/login");
    //   });
    // }
  }

  bool shouldAllowNavigation(Route<dynamic> route) {
    // 根据路由名称或其他条件判断是否允许导航
    if (route.settings.name == '/mine/questionBank') {
      // 示例：不允许导航到问题库页面
      return false;
    }
    return true;
  }
}

bool isNeedLogin(String pageName) {
  return dataUtilComm.dataModel.visitorsType &&
      _needLoginPages.contains(pageName);
}

class MyRouteAware extends RouteAware {
  @override
  void didPopNext() {
    super.didPopNext();
  }

  @override
  void didPushNext() {
    super.didPushNext();
  }
}
